import Vue from 'vue'
import vuex from 'vuex'

Vue.use(vuex)

const store = new vuex.Store({
  strict: true,
  state: {
    arr: []
  },
  mutations: {
    setArr (state, arg) {
      state.arr = arg
    },
    setAge () { alert(12) }
  },
  actions: {
    async loadArr ({commit}) {
      let arr = await (await fetch('http://localhost:8081/a')).json()
      commit('setArr', arr)
    }
  },
  getters: {
    arr (state) {
      if (state.arr.length === 0) {
        store.dispatch('loadArr')
      }
      return state.arr
    }
  },
  modules: {
    user: {
      state: {
        name: 'tim',
        age: 0
      },
      mutations: {
        setAge (state, arg) {
          state.age = arg
        }
      },
      actions: {
        setAge ({commit}, arg) {
          commit('setAge', arg)
        }
      },
      getters: {}
    }
  }
})

export default store
